var publicJs = require("../../../../public.js");

Page({
  data: {
    type: 3,
    money: '',
    realName: '',
    phone: wx.getStorageSync("mobile"),
    cycleArray: ['3个月', '6个月', '9个月', '12个月'],
    cycle: 0,
    carModelArray: ['豪华车', '小型车', 'SUV车', 'MPV车', '紧凑型车'],
    carModel: 0,
  },
  onShow: function () {
    if (wx.getStorageSync("userId") !== '') {
      this.setData({
        mobile: wx.getStorageSync("mobile")
      })
    }
  },
  cyclePickerChange: function (e) {
    this.setData({
      cycle: e.detail.value
    })
  },
  carModelPickerChange: function (e) {
    this.setData({
      carModel: e.detail.value
    })
  }, 
  moneyChange: function (e) {
    this.data.money = e.detail.value
  },
  realNameChange: function (e) {
    this.data.realName = e.detail.value
  },
  doLoan: function (e) {
    var that = this;
    if (this.data.money == '') {
      wx.showModal({
        title: '提示',
        content: '请输入借款金额!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }
    
    if (this.data.realName == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的真实姓名!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }
    publicJs.JsonPost("Borrow/save", {
      money: parseInt(this.data.money),
      real_name: this.data.realName,
      cycle: this.data.cycleArray[this.data.cycle],
      phone: wx.getStorageSync("mobile"),
      cartype: this.data.carModelArray[this.data.carModel],
      type: parseInt(this.data.type)
    }, function (data) {
      publicJs.showLoading("正在提交...请稍后...");
      console.log(data);

      if (data !== false) {
        publicJs.closeLoading();
        wx.showModal({
          title: '提示',
          content: '提交成功!',
          showCancel: false,
          success: function (res) {
            that.setData({
              money: '',
              realName: '',
              cycle: 0,
              carModel: 0
            })
          }
        });
      }

    })
  }
})